package com.itheima.health.controller;



import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.NanNvService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/report")
@Slf4j
public class NanNvController {

    @DubboReference
    NanNvService nanNvService;

    /**
     * @Author: 86166
     * @Date: 10:01 2021/1/20
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询男女占比
    */
    @GetMapping("/getMemberSex")
        public Result getMemberSex(){
        List<Map<String,Object>> setmealCount = nanNvService.countByGender();
        log.info("setmealCount: {}",setmealCount);
        //构造  setmealNames
        List<String> setmealNames = new ArrayList<>();
        //遍历  setmealCount
        for (Map<String, Object> elem : setmealCount) {
        log.info("[string_name]: {}",String.valueOf(elem.get("sex")));
//        setmealNames.add(String.valueOf(elem.get("name")));
            //判断是否为1，为1是男，否则为女，还有可能为null，再次判断
            if(String.valueOf(elem.get("name")).equals("1")){
                setmealNames.add("男");
                elem.put("name","男");
            }else if(String.valueOf(elem.get("name")).equals("2")){
                setmealNames.add("女");
                elem.put("name","女");
            }else if(String.valueOf(elem.get("name")).equals("null")){
                setmealNames.add("null");
                elem.put("name","null");
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        //往 setmealNames setmealCount 设值
        map.put("setmealNames",setmealNames);
        map.put("setmealCount",setmealCount);
        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
}